<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ include file="../taglibs.jsp"%>
<%@taglib prefix="staticResource" tagdir="/WEB-INF/tags/header"%>
<staticResource:staticResource></staticResource:staticResource>
<title><fmt:message key="application.title" /></title>
<style type="text/css">
b {
	color: #666;
}
</style>
</head>
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height: 32px; background: #FFF; padding: 0px">
		<div class="navBox">
			<table height="100%">
				<tr>
					<td>
						<img src="${ctx}/static/images/home/icon0.png" />
					</td>
					<td>${set.sysName}&gt;&gt;系统管理&gt;&gt; 用户管理</td>
				</tr>
			</table>
		</div>
	</div>

	<div data-options="region:'center',title:''" style="overflow-x: hidden; padding: 5px;" fit="true">
		<div id="toolbar" style="padding: 3px">
			<!-- 		<div>
				<table>
					<tr>
						<td><span>查询条件:</span></td>
						<td><input id="" name="" type="text"></td>
						<td><a href="#" class="easyui-linkbutton"
							iconCls="icon-search" plain="true" onclick="doSearch()">搜索</a></td>
					</tr>
				</table>
			</div> -->
			<div>
				<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add()">添加</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="edit()">修改</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="deleteAll()">删除</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-link" plain="true" onclick="bindInfras.bind()">绑定设施</a>
			</div>
		</div>
		<div style="float: left; width: 100%; height: auto; padding: 2px">
			<div style="float: left; width: 100%; margin-top: 5px;">
				<table id="dg">
				</table>
			</div>
		</div>
	</div>

	<!--form  -->
	<div id="dlg" class="easyui-dialog" closed="true" data-options="modal:true" style="width: 350px; height: auto;">
		<form id="fm" method="post">
			<input type="hidden" name="userId">
			<table id="table3" width="99%" height="210px">
				<tr>
					<td height="40px" colspan="4" align="center" valign="middle">
						<b>用户信息</b>
					</td>
				</tr>
				<tr>
					<td align="center">用户名</td>
					<td>
						<input class="easyui-validatebox" type="text" name="username" id="username">
					</td>
				</tr>
				<tr>
					<td align="center">初始密码</td>
					<td>
						<input class="easyui-validatebox" type="text" name="password" id="password">
					</td>
				</tr>
				<tr>
					<td align="center">真实姓名</td>
					<td>
						<input class="easyui-validatebox" type="text" name="name">
					</td>
				</tr>
				<tr>
					<td align="center">角色</td>
					<td>
						<input class="easyui-combobox" name="roleId" id="roleId" data-options="url:'${ctx}/system/role/getAll',valueField:'id',editable:false,textField:'text',multiple:false,height:'20',required:true">
					</td>
				</tr>
				<tr>
					<td align="center">重要数据统计</td>
					<td>
						<input type="radio" name="countDisplay" id="" value="1">
						显示
						<input type="radio" name="countDisplay" id="" value="2">
						隐藏
					</td>
				</tr>
				<tr>
					<td align="center">重要实时数据</td>
					<td>
						<input type="radio" name="realdataDisplay" id="" value="1">
						显示
						<input type="radio" name="realdataDisplay" id="" value="2">
						隐藏
					</td>
				</tr>
				<tr>
					<td align="center">实时告警数据</td>
					<td>
						<input type="radio" name="alertDisplay" id="" value="1">
						显示
						<input type="radio" name="alertDisplay" id="" value="2">
						隐藏
					</td>
				</tr>
				<tr>
					<td align="center">工况数据分析</td>
					<td>
						<input type="radio" name="scadaDisplay" id="" value="1">
						显示
						<input type="radio" name="scadaDisplay" id="" value="2">
						隐藏
					</td>
				</tr>
				<tr>
					<td align="center">待审批流程</td>
					<td>
						<input type="radio" name="approvalDisplay" id="" value="1">
						显示
						<input type="radio" name="approvalDisplay" id="" value="2">
						隐藏
					</td>
				</tr>
				<tr>
					<td align="center">管理统计报表</td>
					<td>
						<input type="radio" name="manageDisplay" id="" value="1">
						显示
						<input type="radio" name="manageDisplay" id="" value="2">
						隐藏
					</td>
				</tr>
				<tr>
					<td align="center">工单故障类型分析</td>
					<td>
						<input type="radio" name="hitchDisplay" id="" value="1">
						显示
						<input type="radio" name="hitchDisplay" id="" value="2">
						隐藏
					</td>
				</tr>
				<tr>
					<td align="center">备注</td>
					<td colspan="3">
						<textarea rows="3" cols="3" style="width: 98%; height: 100%" name="remark"></textarea>
					</td>
				</tr>
				<tr align="center">
					<td height="30" colspan="4" align="center">
						<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()">确定</a>
						&nbsp;&nbsp;
						<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
					</td>
				</tr>
			</table>
		</form>
	</div>

	<div id="dlg2" class="easyui-dialog" closed="true" data-options="modal:true" style="width: 350px; height: 150px;" buttons="#subButton">
		<form id="fm2">
			<input type="hidden" name="userId" id="bindUser">
			选择业主：
			<select class="easyui-combobox" name="customerId" style="width: 150px;" data-options="url:'${ctx}/balance/customer/getAll',valueField:'customerId',editable:false,textField:'customerName',multiple:false,height:'auto'"></select>
		</form>
		<div id="subButton">
			<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="save2()">确定</a>
			&nbsp;&nbsp;
			<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg2').dialog('close')">取消</a>
		</div>
	</div>

	<!-- 设施 树-->
	<div id="cus2infras-dlg" class="easyui-dialog" closed="true" data-options="modal:true" style="width: 280px; height: 500px;" buttons="#bindInfras-buttons">
		<form id="cus2infras-dlg-fm" method="post">
			<input type="hidden" name="userId" id="cus2infras_customerId">
			<input type="hidden" name="infrasIds" id="infrasIds">
			<ul id="infrasTree"></ul>
			<div id="bindInfras-buttons">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="bindInfras.save()">确定</a>
				&nbsp;&nbsp;
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#cus2infras-dlg').dialog('close')">取消</a>
			</div>
		</form>
	</div>
</body>


<!-- 实时数据加载 -->
<script type="text/javascript">
	$(function() {
		// 获取浏览器窗口 
		var windowScreen = document.documentElement;
		var h = windowScreen.clientHeight;
		$('#dg').datagrid({
			url : '${ctx}/system/user/list',
			height : (h - 50),
			nowrap : true,
			autoRowHeight : true,
			striped : false,
			collapsible : false,
			fitColumns : true,
			singleSelect:true,
			remoteSort : false,
			pagination : true,//显示分页 
			rownumbers:true,
			idField : 'userId',
			toolbar : $("#toolbar"),
			columns : [ [ {field : 'userId',title : '用户ID',hidden : true},
			              {field : 'username',title : '用户名',width : 10},
			              {field : 'name',title : '真实姓名',width : 10,align : 'left'},
			              {field : 'roleId',title : '角色ID',width : 80,align : 'left',hidden : true}, 
						  {field : 'roleName',title : '当前角色',width : 10,align : 'left',formatter : function(value, row, index) {
								return new Object(row["role"]).roleName;
						  }},
						 {field : 'countDisplay',title : '重要数据统计',width : 10,align : 'center',formatter : function(value, row, index) {
								return formatDisplay(row.countDisplay);
						  }},
						 {field : 'realdataDisplay',title : '重要实时数据',width : 10,align : 'center',formatter : function(value, row, index) {
								return formatDisplay(row.realdataDisplay);
						  }},
						 {field : 'alertDisplay',title : '实时告警数据',width : 10,align : 'center',formatter : function(value, row, index) {
								return formatDisplay(row.alertDisplay);
						  }},
						 {field : 'scadaDisplay',title : '工况数据分析',width : 10,align : 'center',formatter : function(value, row, index) {
								return formatDisplay(row.scadaDisplay);
						  }},
						 {field : 'approvalDisplay',title : '待审批流程',width : 10,align : 'center',formatter : function(value, row, index) {
								return formatDisplay(row.approvalDisplay);
						  }},
						 {field : 'manageDisplay',title : '管理统计报表',width : 10,align : 'center',formatter : function(value, row, index) {
								return formatDisplay(row.manageDisplay);
						  }},
						 {field : 'hitchDisplay',title : '工单故障类型分析',width : 10,align : 'center',formatter : function(value, row, index) {
								return formatDisplay(row.hitchDisplay);
						  }},
						 {field : 'remark',title : '备注',width : 20,align : 'left',hidden:true}
					  ] ]
		}); 
	});
	var url;
	//add
	function add() {
		$('#dlg').dialog('open').dialog('setTitle', '添加用户');
		$('#fm').form('reset');
		$("input[type=radio][name=countDisplay][value=2]").attr("checked",true);
		$("input[type=radio][name=realdataDisplay][value=2]").attr("checked",true);
		$("input[type=radio][name=alertDisplay][value=2]").attr("checked",true);
		$("input[type=radio][name=scadaDisplay][value=2]").attr("checked",true);
		$("input[type=radio][name=approvalDisplay][value=2]").attr("checked",true);
		$("input[type=radio][name=manageDisplay][value=2]").attr("checked",true);
		$("input[type=radio][name=hitchDisplay][value=2]").attr("checked",true);
		url = '${ctx}/system/user/save';
	}

	//edit
	function edit() {
		var rows = $('#dg').datagrid('getSelections');
		if (rows.length != 1) {
			$.messager.alert('提示', '请选择一条记录', 'info');
		} else {
			$('#dlg').dialog('open').dialog('setTitle', '修改用户');
			$('#fm').form('load', rows[0]);
			url = '${ctx}/system/user/update';
		}
	}
	//save
	function save() {
		$('#fm').form('submit', {
			url : url,
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(response) {
				response = eval('(' + response + ')');
				$.messager.show({
					title : response.title,
					msg : response.msg,
					timeout : response.timeout,
					showType : response.showType
				});
				$('#dlg').dialog('close');
				$('#dg').datagrid('reload');
			}
		});
	}
	//deleteAll
	function deleteAll() {
		var rows = $('#dg').datagrid('getSelections');
		if (null == rows || rows.length < 1) {
			$.messager.alert('提示', '请选择一条记录', 'info');
		} else {
			$.messager.confirm('提示', '确认删除选中的记录吗?', function(r) {
				//获取datagrid选中行
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].userId);
				}
				if (r) {
					$.post('${ctx}/system/user/deleteAll?ids=' + ids, function(response) {
						$.messager.show({
							title : response.title,
							msg : response.msg,
							timeout : response.timeout,
							showType : response.showType
						});
						$('#dlg').dialog('close');
						$('#dg').datagrid('reload');
					}, 'json');
				}
			});
		}
	}
	
	var bindInfras={
		bind:function(){
			var rows = $('#dg').datagrid('getSelections');
			if(rows.length!=1){
				$.messager.alert('提示','至少选择一条数据！','info');
			}else{
				$("#infrasTree").tree({
					url:'${ctx}/monitor/tree/infrasTree3',
					animate:true,
					checkbox:true,
					onLoadSuccess:function(){ 
						 //绑定设施
	               		$.ajax({  
		                   	url:'${ctx}/system/role/selectRole2Infras?userId='+rows[0].userId,  
		                   	cache:false,  
		                   	dataType:'json',  
		                   	success:function(data){  
		                 	  	//默认选中已经关联的菜单
		                  	  	if(data.length>0){
		                  		 	var node;
		                         	for(var i=0;i<data.length;i++){  
		                        	 	node = $('#infrasTree').tree('find',data[i].infrasId);  
		                        	 	if($('#infrasTree').tree('isLeaf',node.target)){	 	                        		 
		                             	 	$('#infrasTree').tree('check',node.target);  
		                        	 	}
		                        	}  
		                 		}   
		                  	}  
		               });   
					}
				});
				$('#cus2infras-dlg').dialog('open').dialog('setTitle','绑定基础设施');
				$("#cus2infras_customerId").val(rows[0].userId);
			}
		},
		save:function(){
			var node = $('#infrasTree').tree('getChecked', ['checked','indeterminate']);
			var infrasIds=[];
			for(var i=0;i<node.length;i++){
				if(node[i].parentId=='infras'){
					infrasIds.push(node[i].id);
				}
	 	 	}
			$("#infrasIds").val(infrasIds);
			$("#cus2infras-dlg-fm").form('submit',{
				url:'${ctx}/system/role/saveBindIfras',
				onSubmit: function(){
					return $(this).form('validate');
				},
				success :function(response) {
					//绑定成功后需要退出重新登录后生效
					//response=eval('('+response+')');
					$.messager.show({
						title:response.title,
						msg:response.msg,
						timeout:response.timeout,
						showType:response.showType
					});
					$("#cus2infras-dlg").dialog('close');
				}
			}); 
		}
	};
	
	function bindCustomer(){
		var rows = $('#dg').datagrid('getSelections');
		if (null == rows || rows.length < 1) {
			$.messager.alert('提示', '请选择一条记录', 'info');
		} else {
			$('#fm2').form('load', rows[0]);
			if(rows[0].customerId==null){
				$("#dlg2").dialog('setTitle','绑定业主').dialog('open');				
			}else{
				$.messager.confirm('提示', '该用户已被绑定，是否修改?', function(r) {
					if(r){						
						$("#dlg2").dialog('setTitle','修改绑定业主').dialog('open');	
					}
				});
			}
		}
	}
	
	function save2(){
		$('#fm2').form('submit', {
			url : '${ctx}/system/user/update',
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(response) {
				response = eval('(' + response + ')');
				$.messager.show({
					title : response.title,
					msg : response.msg,
					timeout : response.timeout,
					showType : response.showType
				});
				$('#dlg2').dialog('close');
				$('#dg').datagrid('reload');
			}
		});
	}
	//search
	function doSearch() {
		$('#dg').datagrid('load', {});
	}
	
	function  formatDisplay(key){
		if(key=='1'){
			return "显示";
		}
		return "隐藏";
	}
</script>
</html>
